<template>
  <div class="goods-box w-card">
    <div class="content-box">
      <div class="title-box">
        <span>{{ incomForm.channelName || '' }}</span>
        <el-button style="margin-left:auto;" @click="inUpload">导入上游商品</el-button>
        <el-button type="primary" @click="addGoods('add',null)">新增上游商品</el-button>
      </div>
      <div class="search-box">
        <div>
          <span>上游商品:</span>
          <el-input v-model="searchForm.channelProductName" clearable placeholder="请输入上游商品名称" />
        </div>
        <div>
          <span>类型:</span>
          <el-select style="width:100%" v-model="searchForm.typeId" filterable clearable @change="typeChange">
            <el-option v-for="item in typeOptions" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
          </el-select>
        </div>
        <div>
          <span>分类:</span>
          <el-select style="width:100%" v-model="searchForm.categoryId" filterable clearable no-data-text="请先选择类型"
            @change="cateChange">
            <el-option v-for="item in simpleOptions" :key="item.categoryId" :label="item.categoryName" :value="item.categoryId" />
          </el-select>
        </div>
      </div>
      <div class="search-box">
        <div>
          <span>品牌:</span>
          <el-select style="width:100%" v-model="searchForm.brandId" filterable clearable no-data-text="请先选择分类"
            @change="brandChange">
            <el-option v-for="item in brandOptions" :key="item.brandId" :label="item.brandName" :value="item.brandId" />
          </el-select>
        </div>
        <div>
          <span>关联商品:</span>
          <el-select style="width:100%" v-model="searchForm.goodsId" filterable clearable no-data-text="请先选择品牌">
            <el-option v-for="item in goodsOptions" :key="item.goodsId" :label="item.goodsName" :value="item.goodsId" />
          </el-select>
        </div>
        <el-button style="margin-left:0;" type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>

      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="channelProductName" label="上游商品名称" align="center">
            <template v-slot="scope">
              {{ scope.row.channelProductName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="shopName" label="品类" align="center" min-width="200">
            <template v-slot="scope">
              <span>{{ scope.row.brandName }}</span>
              <span>【{{ scope.row.typeName + '-' + scope.row.categoryName }}】</span>
            </template>
          </el-table-column>
          <el-table-column prop="channelProductInfo" label="商品信息" align="center">
            <template v-slot="scope">
              {{ scope.row.channelProductInfo || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="channelPrice" label="结算价格(元)" align="center" min-width="120">
            <template v-slot="scope">
              {{ numFormat(scope.row.channelPrice) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="goodsNum" label="关联商品数量" align="center">
            <template v-slot="scope">
              <el-tooltip class="box-item" :content="scope.row.goodsNameList.join(',')" effect="dark" placement="right">
                <span v-show="scope.row.goodsNum != 0">{{ scope.row.goodsNum }}</span>
              </el-tooltip>
              <span v-show="scope.row.goodsNum == 0">{{ scope.row.goodsNum }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="operations" label="操作" align="center" width="160">
            <template v-slot="scope">
              <el-link style="font-size:14px;" type="primary" @click="addGoods('edit',scope.row)">编辑</el-link>
              <el-link style="font-size:14px;margin-left:10px;" type="danger"
                @click="deleteHandle(scope.row.recordId)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <div>
          </div>
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <edit-dialog ref="editDialogRef" @dialogClose="dialogClose" />
  <confirm-pass ref="confirmPassRef" @verification="verification" />
  <in-out-dialog ref="inOutDialogRef" @dialogClose="inOutClose" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import api from '../api'
import { getWindowWidthAndHeight, warningDeal, getDateTime, numFormat } from '@/utils/utils'
import editDialog from './components/editDialog.vue'
import confirmPass from '@/components/confirmPass.vue'
import inOutDialog from './components/inOutDialog.vue'
export default {
  components: {
    editDialog,
    confirmPass,
    inOutDialog,
  },
  setup() {
    const route = useRoute()
    const router = useRouter()

    const editDialogRef = ref(null)
    const confirmPassRef = ref(null)
    const inOutDialogRef = ref(null)

    const state = reactive({
      deleteId: null,
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      incomForm: {},
      searchForm: {},
      tableData: [],

      typeOptions: [],
      simpleOptions: [],
      brandOptions: [],
      goodsOptions: [],
    })

    function typeChange(e) {
      state.searchForm.categoryId = null
      state.searchForm.brandId = null
      state.searchForm.goodsId = null
      if (!e) {
        state.simpleOptions = []
        state.brandOptions = []
        state.goodsOptions = []
        return
      }
      getSimOptions(e)
    }

    function cateChange(e) {
      state.searchForm.brandId = null
      state.searchForm.goodsId = null
      if (!e) {
        state.brandOptions = []
        state.goodsOptions = []
        return
      }
      goodsBrandSimple(e)
    }

    function brandChange(e) {
      state.searchForm.goodsId = null
      if (!e) {
        state.goodsOptions = []
        return
      }
      getGoodsOption(e)
    }

    function getGoodsOption(brandId) {
      const obj = {
        brandId
      }
      api.goodsInfoList(obj).then(res => {
        if (res.code == '200') {
          state.goodsOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getSimOptions(typeId) {
      const obj = {
        typeId
      }
      api.goodsCategoryListSimple(obj).then(res => {
        if (res.code == '200') {
          state.simpleOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getTypeOptions() {
      api.goodsTypeListSimple().then(res => {
        if (res.code == '200') {
          state.typeOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function goodsBrandSimple(categoryId) {
      const obj = {
        categoryId
      }
      api.goodsBrandSimple(obj).then(res => {
        if (res.code == '200') {
          state.brandOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addGoods(type, data) {
      editDialogRef.value.dialogShow(type, { ...data, channelCode: state.incomForm.channelCode })
    }

    function inUpload() {
      inOutDialogRef.value.dialogShow()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        channelCode: state.incomForm.channelCode,
        ...state.searchForm,
      }
      api.productList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function dialogClose() {
      getTableData()
    }

    function deleteHandle(recordId) {
      state.deleteId = recordId
      const title = '删除提示'
      const msg = '是否确定删除该上游商品？'
      confirmPassRef.value.dialogShow(title, msg, false)
    }

    function verification() {
      deleteGoods(state.deleteId)
    }

    function deleteGoods(recordId) {
      const obj = {
        recordId
      }
      api.productRemove(obj).then(res => {
        if (res.code == '200') {
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function inOutClose() {
      getTableData()
    }

    onMounted(() => {
      state.incomForm = route.query || {}
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 340
      getTableData()
      getTypeOptions()
    })

    return {
      ...toRefs(state),
      editDialogRef,
      confirmPassRef,
      inOutDialogRef,
      addGoods,
      cateChange,
      typeChange,
      searchHandle,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      resetHandle,
      brandChange,
      dialogClose,
      deleteHandle,
      verification,
      numFormat,
      inUpload,
      inOutClose,
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
  }
}
.title-box {
  display: flex;
  align-items: center;
  > span {
    font-size: 16px;
    font-weight: bold;
  }
}
.table-box {
  margin-top: 10px;
  .table-footer {
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.search-box {
  display: flex;
  align-items: center;
  gap: 20px;
  box-sizing: border-box;
  padding: 5px;
  margin-top: 10px;
  > div {
    display: flex;
    width: 25%;
    align-items: center;
    gap: 10px;
    > span {
      flex-shrink: 0;
      font-size: 14px;
    }
  }
}
</style>